<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-sys__dict">
      <el-form
        :inline="true"
        :model="dataForm"
        @keyup.enter.native="getDataList()"
      >
        <!-- <el-form-item>
                <el-select v-model="dataForm.homestayId" placeholder="关联民宿" clearable style="width:100%">
                    <el-option v-for="(item,index) in homestays" :key="index" :label="item.name" :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item> -->
        <el-form-item>
          <el-select
            v-model="dataForm.islandId"
            placeholder="所属区域"
            clearable
            style="width: 100%"
          >
            <el-option
              v-for="item in scenicOptions"
              :key="item.id"
              :label="item.scenicName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="dataForm.status"
            placeholder="请选择状态"
            clearable
          >
            <el-option
              v-for="item in status"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="dataForm.adminName"
            placeholder="请输入账号"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="dataForm.linkName"
            placeholder="请输入联系人"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="dataForm.linkPhone"
            placeholder="请输入联系电话"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="dataForm.name"
            placeholder="请输入商户名称"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">{{ $t("query") }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            v-if="$hasPermission('hotel:homestay:save')"
            type="primary"
            @click="addOrUpdateHandle()"
            >{{ $t("add") }}</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button
            v-if="$hasPermission('hotel:homestay::delete')"
            type="danger"
            @click="deleteHandle()"
            >{{ $t("deleteBatch") }}</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            v-if="$hasPermission('hotel:homestay:export')"
            @click="exportHandle()"
            >导出</el-button
          >
        </el-form-item>
      </el-form>
      <el-table
        v-loading="dataListLoading"
        :data="dataList"
        border
        @selection-change="dataListSelectionChangeHandle"
        @sort-change="dataListSortChangeHandle"
        style="width: 100%"
        :height="he"
      >
        <el-table-column
          type="selection"
          header-align="center"
          align="center"
          width="50"
        ></el-table-column>
        <el-table-column
          prop="id"
          label="民宿ID"
          :show-overflow-tooltip="true"
          min-width="160"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="islandName"
          label="所属区域"
          header-align="center"
          min-width="120"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="adminName"
          label="账号名称"
          header-align="center"
          align="center"
          min-width="150"
        ></el-table-column>
        <!-- <el-table-column prop="code" label="企业编码" :show-overflow-tooltip="true" align="center" min-width="160"></el-table-column> -->
        <el-table-column
          prop="name"
          label="商户名称"
          sortable="custom"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          min-width="190"
        ></el-table-column>
        <el-table-column
          prop="legalPerson"
          label="企业法人"
          header-align="center"
          align="center"
          width="180"
        ></el-table-column>
        <el-table-column
          prop="rankTop"
          label="排行榜"
          sortable
          header-align="center"
          align="center"
          width="150"
        ></el-table-column>
        <el-table-column
          prop="createDate"
          label="推荐值"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          min-width="220"
        >
          <template slot-scope="scope">
            <el-input
              style="width: 120px; transform: scale(0.8)"
              v-model="scope.row.recommendVal"
            ></el-input>
            <i
              class="el-icon-check"
              v-if="$hasPermission('hotel:homestay:updRecommendVal')"
              @click="confirmVal(scope.row)"
            ></i>
          </template>
        </el-table-column>
        <el-table-column
          prop="createDate"
          label="地址"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          min-width="220"
        >
          <template slot-scope="scope">
            {{ scope.row.provinceName || "" }}{{ scope.row.cityName || ""
            }}{{ scope.row.areaName || "" }}{{ scope.row.street || ""
            }}{{ scope.row.areaDetail }}</template
          ></el-table-column
        >
        <el-table-column
          prop="linkName"
          label="联系人姓名"
          header-align="center"
          align="center"
          width="180"
        ></el-table-column>
        <el-table-column
          prop="linkPhone"
          label="联系人号码"
          header-align="center"
          align="center"
          width="180"
        ></el-table-column>
        <el-table-column
          prop="applyTime"
          label="申请时间"
          header-align="center"
          align="center"
          width="180"
        ></el-table-column>
        <el-table-column
          prop="checkTime"
          label="审核时间"
          header-align="center"
          align="center"
          width="180"
        ></el-table-column>
        <el-table-column
          prop="status"
          label="状态"
          header-align="center"
          align="center"
          width="180"
        >
          <template slot-scope="scope">
            {{ scope.row.status | delStatus }}
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('handle')"
          fixed="right"
          header-align="center"
          align="center"
          width="150"
        >
          <template slot-scope="scope">
            <el-button
              v-if="
                scope.row.status == 1 &&
                $hasPermission('hotel:homestay:enableUpdate')
              "
              type="text"
              size="small"
              @click="changeStatus(scope.row.id, 0)"
              >禁用</el-button
            >
            <el-button
              v-if="
                scope.row.status == 0 &&
                $hasPermission('hotel:homestay:enableUpdate')
              "
              type="text"
              size="small"
              @click="changeStatus(scope.row.id, 1)"
              >启用</el-button
            >
            <el-button
              v-if="
                scope.row.status == 2 && $hasPermission('hotel:check:audit')
              "
              type="text"
              size="small"
              @click="lookDetail(scope.row.id)"
              >审核</el-button
            >
            <el-button
              type="text"
              size="small"
              v-if="$hasPermission('hotel:homestay:update')"
              @click="addOrUpdateHandle(scope.row.id)"
              >{{ $t("update") }}</el-button
            >
            <el-button
              type="text"
              size="small"
              v-if="$hasPermission('hotel:homestay:delete')"
              @click="deleteHandle(scope.row.id)"
              >{{ $t("delete") }}</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="limit"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle"
      >
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update
        v-if="addOrUpdateVisible"
        ref="addOrUpdate"
        @refreshDataList="getDataList"
      ></add-or-update>
      <!-- 详情 -->
      <look-detail
        v-if="lookDetailVisible"
        ref="lookDetail"
        @refreshDataList="getDataList"
      ></look-detail>
    </div>
  </el-card>
</template>

<script>
var that;
import mixinViewModule from "@/mixins/view-module";
import AddOrUpdate from "./hotel-add-or-update";
import lookDetail from "./hotel-add-or-update2";
export default {
  mixins: [mixinViewModule],
  data() {
    return {
      mixinViewModuleOptions: {
        getDataListURL: "/hotel/homestay/page",
        getDataListIsPage: true,
        deleteURL: "/hotel/homestay",
        deleteIsBatch: true,
        exportURL: "/hotel/homestay/export",
      },
      he:'auto',
      scenicOptions: [],
      homestays: [],
      status: [
        {
          label: "停用",
          value: 0,
        },
        {
          label: "启用",
          value: 1,
        },
        {
          label: "待审核",
          value: 2,
        },
        {
          label: "通过",
          value: 3,
        },
        {
          label: "未通过",
          value: 4,
        },
      ],
      dataForm: {
        operateProject: "",
        linkPhone: "",
        linkName: "",
        code: "",
        adminName: "",
        status: "",
        islandId: "",
      },
    };
  },
  filters: {
    delStatus(val) {
      if (val||val===0) {
        let d = that.status.filter((item) => item.value == val);
        if (d.length != 0) {
          return d[0].label;
        }
      }
    },
  },
  watch:{
    dataList(newval, oldval) {
      if (newval != oldval) {
        this.he = "auto";
        setTimeout(() => {
          this.he = "calc(100vh - 270px)";
        }, 500);
      }
    },
  },
  components: {
    AddOrUpdate,
    lookDetail,
  },
  created() {
    that = this;
    this.getHomestay();
    this.getScenicOptions();
  },
  methods: {
    changeStatus(id, status) {
      this.$confirm(
        `${status ? "启用后会将该商户上架到用户小程序中" : "禁用后会将该商户从用户小程序中下架"}？`,
        this.$t("prompt.title"),
        {
          confirmButtonText: this.$t("confirm"),
          cancelButtonText: this.$t("cancel"),
          type: "warning",
        }
      )
        .then(() => {
          this.$http
            .post("/hotel/homestay/enableUpdate", {
              id: id,
              status: status,
            })
            .then((res) => {
              if (res.data.code !== 0) {
                return this.$message.error(res.data.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.getDataList();
                },
              });
            });
        })
        .catch(() => {
          console.log(1);
        });
    },
    confirmVal(row) {
      this.$http
        .post("/hotel/homestay/updRecommendVal", {
          id: row.id,
          recommendVal: parseFloat(row.recommendVal),
        })
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error(res.data.msg);
          }
          this.$message({
            message: this.$t("prompt.success"),
            type: "success",
            duration: 500,
            onClose: () => {},
          });
        });
    },
    getHomestay() {
      this.$http
        .get("/hotel/homestay/getHomestayDownBox")
        .then(({ data: res }) => {
          if (res.code == 0) {
            this.homestays = res.data;
          }
        });
    },
    getScenicOptions() {
      this.$http
        .get("scenic/isscenic/selectScenicList", {})
        .then(({ data: res }) => {
          if (res.msg == "success") {
            this.scenicOptions = res.data;
          }
        });
    },
  },
};
</script>
